<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/serve/ticket-order' }">购票订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{$router.currentRoute.name}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="车展ID" prop="showId">
        <el-input v-model="ruleForm.showId"></el-input>
      </el-form-item>
      <el-form-item label="车展票价" prop="price">
        <el-input-number v-model="ruleForm.price" :precision="2" :step="0.1" @change="handleChange"></el-input-number>
      </el-form-item>
      <el-form-item label="购票总数" prop="totalNum">
        <el-input-number v-model="ruleForm.totalNum" @change="handleChange" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="购票总价" prop="totalPrice">
        <el-input v-model="ruleForm.totalPrice" disabled></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input v-model="ruleForm.notes"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import global from '../../../Global'
let preTicketOrderUrl = global.preTicketOrders ;

export default {
  data() {
    return {
      ruleForm: {
        totalNum: 1
      },
      rules: {
        showId: [
          { required: true, message: '请输入车展ID', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入票价', trigger: 'blur' }
        ],
        totalNum: [
          { required: true, message: '请输入购票总数', trigger: 'blur' }
        ],
        totalPrice: [
          { required: true, message: '请输入总票价', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleChange(value) {
      this.ruleForm.totalPrice = this.ruleForm.totalNum * this.ruleForm.price;
    },
    //新增
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = preTicketOrderUrl + "/add-new";
          let formData = this.qs.stringify(this.ruleForm);

          let localJwt = localStorage.getItem('localJwt');
          this.axios.create({'headers': {'Authorization': localJwt}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if(jsonResult.state == 20000){
              this.$notify.success({title: '成功',
                message: '添加成功'
              });

              this.resetForm(formName);
            }else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    mounted() {
      this.ruleForm.totalPrice =  this.ruleForm.price * this.ruleForm.totalNum;
    }
  }
}
</script>
